package com.hd.service.impl;

import com.hd.dao.BaseDao;
import com.hd.entity.Attach;
import com.hd.service.AttachService;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 附件
*/
@Service("attachService")
public class AttachServiceImpl implements AttachService {
    @Autowired
    private BaseDao<Attach,String>attachDao;
    @Override
    public void save(Attach attach) {
        attach.setCreateTime(new Date());
       attachDao.save(attach);
    }

    @Override
    public void delete(String id){
      attachDao.delete(Attach.class,id);
    }
       

    @Override
    public void update(Attach attach) {
        Attach vo=attachDao.get(Attach.class,attach.getId());
        attach.setCreateTime(vo.getCreateTime());
        attachDao.update(attach);
    }
    @Override
    public Attach get(String id){
    	return attachDao.get(Attach.class, id);
    }
        

    @Override
    public void selectDataGrid(PageInfo pageInfo) {
        String hql="from Attach where 1=1 ";
        Map<String,Object>condition=pageInfo.getCondition();
        List<Object> params=new ArrayList<Object>();
        if(!pageInfo.getSort().equals("")) {
            hql+=" order by "+pageInfo.getSort()+" "+pageInfo.getOrder();
        }
        List<Attach> attachs=attachDao.findPage(hql, pageInfo.getNowpage(), pageInfo.getPagesize(),params);
        pageInfo.setRows(attachs);
        String countHql="select count(*)  "+hql;
        int total=Integer.parseInt(attachDao.getCountByList(countHql,params).toString());
        pageInfo.setTotal(total);
    }

	@Override
	public Attach findByPath(String path) {
		String hql="from Attach where path=?";
		List<Attach>attachList=attachDao.find(hql,path);
		if(attachList.size()>0) {
			return attachList.get(0);
		}
		return null;
	}

    @Override
    public List<Attach> findInIds(String ids) {
        List<Attach>attachList=new ArrayList<>();
        if(StringUtils.isNotNull(ids)){
            String hql="from Attach where id in("+StringUtils.convertSingleStr(ids)+")";
            attachList=attachDao.find(hql);
        }
        return attachList;
    }

    @Override
    public void updateFileSize(String id, String size) {
        String hql="update Attach set size=? where id=?";
        attachDao.execute(hql,size,id);
    }
}
